<template>
  <div>
    <n-button @click="showAddModal=true">添加</n-button>

    <n-table :bordered="false" :single-line="false">
      <thead>
        <tr>
          <th>编号</th>
          <th>名称</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(i,index) in categortyList">
          <td>{{i.id}}</td>
          <td>{{i.name}}</td>
          <td>
            <n-space>
              <!-- n-space可以自动调试合适的距离 -->
              <n-button @click="toUpdate(i)">修改</n-button>
              <n-button @click="deleteCategory(i)">删除</n-button>
            </n-space>
          </td>


        </tr>

      </tbody>
    </n-table>

    <n-modal v-model:show="showAddModal" preset="dialog" title="Dialog">
      <template #header>
        <div>添加分类</div>
      </template>
      <div>
        <n-input v-model:value="addCategory.name" type="text" placeholder="请输入名称" />
      </div>
      <template #action>
        <div>
          <n-button @click="addMessage">提交</n-button>
        </div>
      </template>
    </n-modal>

    <n-modal v-model:show="showUpdateModal" preset="dialog" title="Dialog">
      <template #header>
        <div>修改分类</div>
      </template>
      <div>
        <n-input v-model:value="updateCategory.name" type="text" placeholder="请输入名称" />
      </div>
      <template #action>
        <div>
          <n-button @click="updateMessage">提交</n-button>
        </div>
      </template>
    </n-modal>
  </div>
</template>

<script setup>
import { AdminStore } from '../../stores/AdminStore'  //1.引入
import { ref, reactive, inject, onMounted } from 'vue'

import { useRouter, useRoute } from 'vue-router'; //路由
const router = useRouter()
const route = useRoute()

const dialog = inject("dialog")
const message = inject("message")
const axios = inject("axiosTool")  //axiosTool 是我provide定义的名字
const adminStore = AdminStore(); //2.实例化
onMounted(() => {
  loadDatas()
})
const addCategory = reactive({
  name: "",
})

const updateCategory = reactive({
  name: "",
  id: 0

})
const categortyList = ref([])
const showAddModal = ref(false);
const showUpdateModal = ref(false);

const loadDatas = async () => {
  let res = await axios.get("/categorty/list")
  categortyList.value = res.data.rows
  console.log(res);

}


const addMessage = async () => {
  let res = await axios.post("/categorty/_token/add", {
    name: addCategory.name
  })
  //  { headers: { token: adminStore.token } }
  console.log(res);
  if (res.data.code == 200) {
    message.info(res.data.msg)
    loadDatas()
  } else {
    message.error(res.data.msg)

  }
  showAddModal.value = false;

}


const updateMessage = async () => {
  let res = await axios.post("/categorty/_token/update", {
    name: updateCategory.name,
    id: updateCategory.id,

  })
  //  { headers: { token: adminStore.token } }
  console.log(res);
  if (res.data.code == 200) {
    message.info(res.data.msg)
    loadDatas()
  } else {
    message.error(res.data.msg)

  }
  showUpdateModal.value = false;

}

const toUpdate = async (i) => {
  showUpdateModal.value = true
  updateCategory.id = i.id
  updateCategory.name = i.name

}


const deleteCategory = async (i) => {
  dialog.warning({
    title: '警告',
    content: '是否要删除',
    positiveText: '确定',
    negativeText: '取消',
    onPositiveClick: async () => {
      // message.success('确定')
      let res = await axios.delete(`/categorty/_token/delete?id=${i.id}`)
      if (res.data.code == 200) {
        loadDatas()
        message.info(res.data.msg)

      } else {
        message.error(res.data.msg)

      }
    },
    onNegativeClick: () => {
    }
  })

}
</script>

<style lang="scss" scoped>

</style>